﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain;
using Service.Interface;
using Dao.Interface;

namespace Service
{
    public class AreaService : Service<Area>, IAreaService
    {
        public IPageRepository PageRepository;

        /// <summary>
        /// 更新分区是否扔进垃圾箱
        /// </summary>
        /// <param name="areaID">分区ID</param>
        /// <param name="delete">是否删除</param>
        public void UpdateAreaMarkDelete(object areaID, bool delete)
        {
            if (areaID == null)
            {
                return;
            }
            Area area = ((IAreaRepository)(this.CurrentRepository)).Get(areaID);
            area.MarkDelete = delete;
            ((IAreaRepository)(this.CurrentRepository)).Update(area);
        }

        /// <summary>
        /// 重写删除，包括删除分区，分区中的页
        /// </summary>
        /// <param name="areaID">分区ID</param>
        public override void Delete(object areaID)
        {
            if (areaID == null)
            {
                return;
            }
            Area area = ((IAreaRepository)(this.CurrentRepository)).Get(areaID);
            //删除分区中的页
            PageRepository.Delete(area.Pages);
            //删除分区
            ((IAreaRepository)(this.CurrentRepository)).Delete(area);
        }

        /// <summary>
        /// 更新分区是否共享
        /// </summary>
        /// <param name="areaID">分区ID</param>
        /// <param name="share">是否共享</param>
        public void UpdateAreaShare(object areaID, bool share)
        {
            Area area = ((IAreaRepository)(this.CurrentRepository)).Get(areaID);
            area.Share = share;
            ((IAreaRepository)(this.CurrentRepository)).Update(area);
            foreach (Page page in area.Pages)
            {
                page.Share = share;
                this.PageRepository.Update(page);
            }
        }

        /// <summary>
        /// 获取分区集合
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <param name="delete">是否删除</param>
        /// <returns></returns>
        public IList<Area> LoadAllArea(object userID, bool delete)
        {
            return ((IAreaRepository)(this.CurrentRepository)).LoadAllArea(userID, delete);
        }

        /// <summary>
        /// 获取共享分区集合
        /// </summary>
        /// <returns></returns>
        public IList<Area> LoadAllShareArea()
        {
            return ((IAreaRepository)(this.CurrentRepository)).LoadAllShareArea();
        }
    }
}
